package com.drone.drone_server.dao;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.drone.drone_server.entity.*;
import com.drone.drone_server.dto.*;
import org.apache.ibatis.annotations.*;

import java.util.List;

// 火情视频DAO
public interface FireVideoDao extends BaseMapper<FireVideo> {
    @Select("SELECT * FROM fire_video WHERE fire_level = #{fireLevel}")
    List<FireVideo> selectByFireLevel(@Param("fireLevel") String fireLevel);

    @Select("SELECT * FROM fire_video WHERE latitude BETWEEN #{minLat} AND #{maxLat} " +
            "AND longitude BETWEEN #{minLng} AND #{maxLng}")
    List<FireVideo> selectByLocationRange(@Param("minLat") Float minLat,
                                          @Param("maxLat") Float maxLat,
                                          @Param("minLng") Float minLng,
                                          @Param("maxLng") Float maxLng);


    @Select("SELECT fv.fire_id AS fireId, " +
            "fv.video_path AS videoPath, " +
            "fv.fire_level AS fireLevel, " +
            "fv.latitude AS latitude, " +
            "fv.longitude AS longitude, " +
            "fv.record_time AS recordTime, " +
            "dfr.drone_id AS droneId, " +
            "dr.route_name AS routeName, " +
            "dfr.start_time AS startTime, " +
            "dfr.end_time AS endTime " +
            "FROM fire_video fv " +
            "LEFT JOIN flight_fire_relation ffr ON fv.fire_id = ffr.fire_id " +
            "LEFT JOIN drone_flight_record dfr ON ffr.record_id = dfr.record_id "+
            "LEFT JOIN drone_route dr ON dr.route_id = dfr.route_id " +
            "WHERE fv.fire_id = #{fireId}")
    @Results({
            @Result(property = "fireId", column = "fireId"),
            @Result(property = "videoPath", column = "videoPath"),
            @Result(property = "fireLevel", column = "fireLevel"),
            @Result(property = "latitude", column = "latitude"),
            @Result(property = "longitude", column = "longitude"),
            @Result(property = "recordTime", column = "recordTime"),
            @Result(property = "droneId", column = "droneId"),
            @Result(property = "routeName", column = "routeName"),
            @Result(property = "startTime", column = "startTime"),
            @Result(property = "endTime", column = "endTime")
    })
    FireExpansionInfo selectFireExpansionInfo(@Param("fireId") Integer fireId);

    @Select("SELECT fv.fire_id AS fireId, " +
            "fv.video_path AS videoPath, " +
            "fv.fire_level AS fireLevel, " +
            "fv.latitude AS latitude, " +
            "fv.longitude AS longitude, " +
            "fv.record_time AS recordTime, " +
            "dfr.drone_id AS droneId, " +
            "dr.route_name AS routeName, " +
            "dfr.start_time AS startTime, " +
            "dfr.end_time AS endTime " +
            "FROM fire_video fv " +
            "LEFT JOIN flight_fire_relation ffr ON fv.fire_id = ffr.fire_id " +
            "LEFT JOIN drone_flight_record dfr ON ffr.record_id = dfr.record_id "+
            "LEFT JOIN drone_route dr ON dr.route_id = dfr.route_id " )
    @Results({
            @Result(property = "fireId", column = "fireId"),
            @Result(property = "videoPath", column = "videoPath"),
            @Result(property = "fireLevel", column = "fireLevel"),
            @Result(property = "latitude", column = "latitude"),
            @Result(property = "longitude", column = "longitude"),
            @Result(property = "recordTime", column = "recordTime"),
            @Result(property = "droneId", column = "droneId"),
            @Result(property = "routeName", column = "routeName"),
            @Result(property = "startTime", column = "startTime"),
            @Result(property = "endTime", column = "endTime")
    })
    List<FireExpansionInfo> selectFireExpansionInfos();

    @Select("SELECT pfh.handle_time AS handleTime, " +
            "pp.person_name AS personName " +
            "FROM fire_video fv " +
            "LEFT JOIN person_fire_handling pfh ON pfh.fire_id = fv.fire_id " +
            "LEFT JOIN person_position pp ON pp.person_phone_number = pfh.person_phone_number " +
            "WHERE fv.fire_id = #{fireId}")
    @Results({
            @Result(property = "handleTime", column = "handleTime"),
            @Result(property = "personName", column = "personName")
    })
    fireHandleRecord selectFireHandRecord(@Param("fireId") Integer fireId);

}